<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Show extends Model
{
    use HasFactory;

    protected $table = 'show';

    protected $primaryKey = 'id';

    /**
     * @param array $params
     * @return array
     */
    public function getList(array $params): array
    {
        $whereSql = self::conditions($params);

        $sql = "
            SELECT
                s.*
            FROM
                `show` s
            WHERE
                s.delete_flg = 0
                {$whereSql}
        ";
        return DB::select($sql);
    }

    public static function conditions(array $params): string
    {
        $whereSql = "";

        if (!empty($params['type'])) {
            $whereSql .= "and s.type = '{$params['type']}'";
        }
        if (!empty($params['skip_type'])) {
            $whereSql .= "and s.skip_type = '{$params['skip_type']}'";
        }
        if (!empty($params['status'])) {
            $whereSql .= "and s.status = '{$params['status']}'";
        }
        if (!empty($params['start_at'])) {
            $whereSql .= "and s.start_at >= '{$params['start_at']}'";
        }
        if (!empty($params['end_at'])) {
            $whereSql .= "and s.end_at <= '{$params['end_at']}'";
        }

        return $whereSql;
    }
}
